package com.googlecode.lookie.platform.prefs;

import java.awt.BorderLayout;

import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import org.springframework.richclient.preference.PreferencePage;

import com.googlecode.lookie.platform.settings.SettingsIds;

public class GeneralPreferencePage
    extends PreferencePage
{

    private JCheckBox showTipsCheckBox;

    private JCheckBox useNativeIconsCheckBox;

    private JLabel thumbSizeLabel;

    private JSlider thumbSizeSlider;

    public GeneralPreferencePage()
    {
        super( "generalPreferencePage" );
    }

    @Override
    protected JComponent createContents()
    {
        showTipsCheckBox = new JCheckBox( "Show Tips at Startup" );
        useNativeIconsCheckBox = new JCheckBox( "Use Native Icons" );
        thumbSizeLabel = new JLabel( "Thumbnail Size" );
        createThumbSizeSlider();

        onAboutToShow();

        GeneralPreferencePanel panel = new GeneralPreferencePanel( new GeneralPreferencePanelParams()
        {

            public JLabel getThumbSizeLabel()
            {
                return thumbSizeLabel;
            }

            public JSlider getThumbSizeSlider()
            {
                return thumbSizeSlider;
            }

            public JCheckBox getShowTipsCheckBox()
            {
                return showTipsCheckBox;
            }

            public JCheckBox getUseNativeIconsCheckBox()
            {
                return useNativeIconsCheckBox;
            }

        } );

        JPanel wrapper = getComponentFactory().createPanel( new BorderLayout() );
        wrapper.add( panel, BorderLayout.CENTER );

        return wrapper;
    }

    private void createThumbSizeSlider()
    {
        thumbSizeSlider = new JSlider();
        thumbSizeSlider.setModel( new DefaultBoundedRangeModel( 100, 0, 100, 300 ) );
    }

    @Override
    protected void onDefaults()
    {
        boolean showTips = getSettings().getDefaultBoolean( SettingsIds.SHOW_TIPS );
        showTipsCheckBox.setSelected( showTips );

        boolean useNativeIcons = getSettings().getDefaultBoolean( SettingsIds.USE_NATIVE_ICONS );
        useNativeIconsCheckBox.setSelected( useNativeIcons );

        int thumbSize = getSettings().getDefaultInt( SettingsIds.THUMB_SIZE );
        thumbSizeSlider.getModel().setValue( thumbSize );
    }

    @Override
    protected boolean onFinish()
    {
        getSettings().setBoolean( SettingsIds.SHOW_TIPS, showTipsCheckBox.isSelected() );
        getSettings().setBoolean( SettingsIds.USE_NATIVE_ICONS, useNativeIconsCheckBox.isSelected() );
        getSettings().setInt( SettingsIds.THUMB_SIZE, thumbSizeSlider.getModel().getValue() );

        return super.onFinish();
    }

    public void onAboutToShow()
    {
        boolean showTips = getSettings().getBoolean( SettingsIds.SHOW_TIPS );
        showTipsCheckBox.setSelected( showTips );

        boolean useNativeIcons = getSettings().getBoolean( SettingsIds.USE_NATIVE_ICONS );
        useNativeIconsCheckBox.setSelected( useNativeIcons );

        int thumbSize = getSettings().getInt( SettingsIds.THUMB_SIZE );
        thumbSizeSlider.getModel().setValue( thumbSize );
    }

}
